在上一篇我們已經完成信件發送範例程式的測試,我們現在要從Queue裡面取出之前送進來的新會員資料,再發送信件給我
廢話不多說,直接進入正題,直接來寫程式
為了導入Azure Storage的套件,必須加上這幾行在前面
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
宣告CloudStorageAccount,從環境變數存取對應儲存體服務
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
Environment.GetEnvironmentVariable("StorageConnectionString"));
須點選function app,在點選[應用程式設定Application Settings]
取得Azure Queue的 CloudQueueClient 物件。
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("memberhobby");
查看訊息但不刪除
CloudQueueMessage messageQueue = queue.PeekMessage();
取得訊息,從Queue中移除
CloudQueueMessage retrievedMessage = queue.GetMessage();
queue.DeleteMessage(retrievedMessage);
#r "SendGrid"
#r "Microsoft.WindowsAzure.Storage"
using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
public static Mail Run(TimerInfo myTimer, TraceWriter log)
{
var today = DateTime.Today.ToShortDateString();
log.Info($"Generating daily report for {today} at {DateTime.Now}");
Mail message = new Mail()
{
Subject = $"Daily Report for {today}"
};
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("memberhobby");
// Peek at the next message
CloudQueueMessage messageQueue = queue.PeekMessage();
// Display message.
log.Info(messageQueue.AsString);
// Get the next message
CloudQueueMessage retrievedMessage = queue.GetMessage();
//Process the message in less than 30 seconds, and then delete the message
queue.DeleteMessage(retrievedMessage);
Content content = new Content
{
Type = "text/plain",
Value = retrievedMessage.AsString
};
message.AddContent(content);
return message;
}
執行程式碼後,確實收到信件
Message也從Queue裡被移除了
寫完程式碼是不是覺得頭暈暈的呢?在下一篇開始,我想教大家一個有趣的Azure Service!
偷偷告訴大家,這個Service是我的最愛--Logic App!它甚至可以不用寫程式碼,就串接許多Azure的服務及第三方API,真的是非常的好用喔!
迫不及待的話,快速連結在此:Azure Logic Apps介紹-及使用Azure Logic Apps打造交友新創公司服務
參考: